<template>
	<view class="content">
		<u-navbar title="发布动态" :placeholder='true' height="88rpx" leftIconColor="#fff"
			titleStyle='color:#fff;font-size:32rpx' left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true">
		</u-navbar>
		<view class="release_area">
			<textarea name="release_text" v-model="releaseText" placeholder-class="release_text_color" placeholder="这一刻的想法..."
				id="" cols="30" rows="10"></textarea>
			<view class="release_image">
				<u-upload width="180rpx" uploadIcon="plus" uploadText="上传图片" height="180rpx" :fileList="fileList1"
					accept="image" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="3"></u-upload>
			</view>
		</view>
		<view class="select_relevance">
			<view class="select_top" @click="toSelectGoods">
				<view class="left">
					<image src="../../static/images/release-dynamic/Rectangle 4@2x (4).png" mode=""></image>
					选择关联商品（选填）
				</view>
				<view class="right_arrow">
					<image src="../../static/images/release-dynamic/Group@2x.png" mode=""></image>
				</view>
			</view>
			<view class="select_goods" v-if="circleGoods!= -1">
				<relevance-goods :item="goodsInfo"></relevance-goods>
			</view>
			<view class="select_top select_bottm" @click="toLoction">
				<view class="left">
					<image src="../../static/images/release-dynamic/Rectangle 4@2x (5).png" mode=""></image>
					选择位置
				</view>
				<view class="address">
					{{circleAddress}}
				</view>
				<view class="right_arrow">
					<image src="../../static/images/release-dynamic/Group@2x.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="issue_button" @click="issueClick">
			发布动态
		</view>
	</view>
</template>

<script>
	import {
		issue,
		getCommodityInfo
	} from '../../api/index.js'
	import baseUrl from '../../utils/base.js'
	export default {
		onShow() {
			this.circleAddress = this.$store.state.circleAddress
			if (!this.circleAddress) {
				this.circleAddress = ''
			}
			this.circleGoods = this.$store.state.circleGoods
			if (this.circleGoods != -1) {
				this.getGoodsInfo(this.circleGoods)
			}
		},
		data() {
			return {
				titleStyle: {
					color: '#fff',
				},
				fileList1: [],
				circleAddress: '',
				goodsInfo: {},
				circleGoods: -1,
				urls: [],
				releaseText: ''
			}
		},
		methods: {
			// 发布圈子
			async issueCircle(data) {
				console.log(data);
				const res = await issue(data)
				console.log(res);
				if (res.code != 200) {
					return
				}
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					icon: 'none',
					success() {
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/factionalism/factionalism'
							})
						}, 1000)
					}
				})
			},
			issueClick() {
				if (this.circleAddress == '') {
					uni.showToast({
						title: '请选择地址',
						icon: 'error'
					})
					return
				}
				if (!this.goodsInfo.commodityName) {
					uni.showToast({
						title: '请选择商品',
						icon: 'error'
					})
					return
				}
				if (this.releaseText == '') {
					uni.showToast({
						title: '请输入文字内容',
						icon: 'error'
					})
					return
				}
				uni.showLoading({
					title: '发布中...',
					mask: true
				})
				if (this.fileList1.length) {

					this.fileList1.forEach(async (item, index) => {
						// console.log(item);
						const res = await this.uploadFilePromise(item.url)
						console.log(res);
						if (JSON.parse(res.data).code == 200) {
							this.urls.push(JSON.parse(res.data).msg)
							if ((index + 1) == this.fileList1.length) {
								console.log('最后一次');
								setTimeout(() => {
									this.issueCircle({
										userId: this.$store.state.userInfo.id,
										nickName: this.$store.state.user.name,
										avatar: this.$store.state.user.avatar,
										loca: this.circleAddress,
										commodityName: this.goodsInfo.commodityName,
										text: this.releaseText,
										other: this.urls.join()
									}, 800)
								})
							}
						}
					})
					return
				}
				this.issueCircle({
					userId: this.$store.state.userInfo.id,
					nickName: this.$store.state.user.name,
					avatar: this.$store.state.user.avatar,
					loca: this.circleAddress,
					commodityName: this.goodsInfo.commodityName,
					text: this.releaseText,
					other: this.urls.join()
				})
			},
			async getGoodsInfo(id) {
				const res = await getCommodityInfo(id)
				// console.log(res);
				this.goodsInfo = res.data
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				// console.log(event.file);
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseUrl + '/common/uploadOss',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(res)
						}
					});
				})
			},
			toSelectGoods() {
				uni.navigateTo({
					url: '/pages/select-goods/select-goods'
				})
			},
			toLoction() {
				uni.navigateTo({
					url: '/pages_mine/pages/select-location/select-location'
				})
			},
			close() {
				console.log(1);
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;
		// padding-bottom: 100rpx;

		.release_area {
			margin-top: 20rpx;
			padding: 26rpx 30rpx;
			background-color: #fff;
			margin-bottom: 38rpx;

			.release_text_color {
				color: #d4d4d4;
			}

			textarea {
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
				height: 120rpx;
				margin-bottom: 6rpx;
			}

			.release_image {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				padding-top: 20rpx;

			}
		}

		.select_relevance {
			background-color: #fff;

			.select_top {
				border-bottom: 1rpx solid rgba(119, 119, 119, 0.3);
				// height: 90rpx;
				padding: 10rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: center;
					padding-left: 30rpx;
					height: 90rpx;


					image {
						width: 58rpx;
						height: 58rpx;
						display: block;
						margin-right: 20rpx;
					}
				}

				.address {
					width: 432rpx;
				}

				.right_arrow {
					padding-right: 30rpx;

					image {
						width: 10rpx;
						height: 18rpx;
					}
				}
			}

			.select_goods {
				border-bottom: 1rpx solid rgba(119, 119, 119, 0.3);
				padding: 20rpx 30rpx;
			}

			.select_bottm {

				.right_arrow {
					.address {}

					image {
						margin-left: 10rpx;
					}
				}
			}
		}

		.issue_button {
			width: 600rpx;
			height: 68rpx;
			background: #FF594D;
			border-radius: 104rpx 104rpx 104rpx 104rpx;
			position: fixed;
			left: 50%;
			transform: translateX(-50%);
			bottom: 30rpx;
			font-size: 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			text-align: center;
			line-height: 68rpx;
		}
	}
</style>